#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define high 25
#define width 35
int picture[high][width]={0};
int long_s=4;
int to;
int food_x,food_y;
int goal;

void gotoxy(int x,int y) //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void move()
{
    int i,j;
    for(i=0;i<high-1;i++)
    {
        for(j=0;j<width-1;j++)
        {
            if(picture[i][j]>0)
            picture[i][j]++;
        }
    }
    int tail_x,tail_y;
    int head_x,head_y;
    int max=0;
    for(i=0;i<high-1;i++)
    {
           for(j=0;j<width-1;j++)
           {
               if(picture[i][j]>1)
            {
                if(max<picture[i][j])
                {
                    max=picture[i][j];
                    tail_x=i;
                    tail_y=j;
                }
                if(picture[i][j]==2)
                {
                    head_x=i;
                    head_y=j;
                }
            }
           }
    }
    int nhead_x,nhead_y;
    if(to==1)
    {
        nhead_x=head_x-1;
        nhead_y=head_y;
    }
    if(to==2)
    {
        nhead_x=head_x+1;
        nhead_y=head_y;
    }
    if(to==3)
    {
        nhead_x=head_x;
        nhead_y=head_y-1;
    }
    if(to==4)
    {
        nhead_x=head_x;
        nhead_y=head_y+1;
    }
    if(nhead_x==food_x&&nhead_y==food_y)
    {
        goal++;
        picture[food_x][food_y]=0;
        food_x=rand()%(high-2)+2;
        food_y=rand()%(width-2)+2;
        picture[food_x][food_y]=-2;
    }
    else
    {
        picture[tail_x][tail_y]=0;
    }
    if(nhead_x==0||nhead_y==0||nhead_x==high||nhead_y==width||picture[nhead_x][nhead_y]>0)
    {
        printf("最终的得分为",goal);
        exit(0);
    }
    picture[nhead_x][nhead_y]=1;
}

void start()
{
    int i,j;// 初始化边框
    for (i=0;i<high;i++)
    {
        picture[i][0] = -1;
        picture[i][width-1] = -1;
    }
    for (j=0;j<width;j++)
    {
        picture[0][j] = -1;
        picture[high-1][j] = -1;
    } 	 	// 初始化蛇头位置
    picture[high/2][width/2] = 1; 	// 初始化蛇身，画布中元素值分别为2,3,4,5....
    for (i=1;i<=long_s;i++)
    picture[high/2][width/2-i] = i+1;
    food_x=rand()%high;
    food_y=rand()%width;
    picture[food_x][food_y]=-2;
    to=4;
    goal=0;
}

void show()
{
    gotoxy(0,0);
    int i,j;
    for (i=0;i<high;i++)
    {
        for (j=0;j<width;j++)
        {
            if (picture[i][j]==0)
                printf(" "); // 输出空格
            else if(picture[i][j]==-2)
                printf("O");//输出食物。
            else if (picture[i][j]==-1)
                printf("#"); // 输出边框#
            else if (picture[i][j]==1)
                printf("@"); // 输出蛇头@
            else if (picture[i][j]>1)
                printf("*"); // 输出蛇身*
        }
        printf("\n");
    }
    Sleep(99);
}

void input()
{
    char c;
    if(kbhit()) // 判断是否有输入
    {
        c = getch(); // 根据用户的不同输入来移动，不必输入回车
        if (c == 'a')
        {
            to = 3; // 位置左移
            move();
        }
        else if (c == 'd')
        {
            to = 4; // 位置右移
            move();
        }
        else if (c== 'w')
        {
            to = 1; // 位置上移
            move();
        }
        else if (c == 's')
        {
            to = 2; // 位置下移
            move();
        }
    }
}

void outinput()
{
    move();
}

int  main()
{
    start();
    while(1)
    {
        show();
        input();
        outinput();
        printf("当前得分为%d",goal);
    }
    return 0;
}









